import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

/*
 * Name: DrawPanel.java 
 * Function: Draws a sundial and gnomon based on information gotten from a Calculation object parameter.  
 *
 */

public class DrawPanel extends JPanel{
        
        private Calculation calc;
         
 /* Name: DrawPanel(Calculation c) 
  * Description: Constructor for a DrawPanel object
  * Param: Calculation c
  * Returns: Nothing
   */
        
        public DrawPanel(Calculation c){
                
            calc = c;
            this.setSize(612,792);
            this.setBackground(Color.WHITE);
            this.setVisible(true);
            setBackground(Color.white);
            setPreferredSize(new Dimension(612, 792));
        }
        
 /* 
  * Name: paintComponent(Graphics g)
  * Description: Paints a sundial and gnomon to a Graphics object.
  * Param: Graphics g
  * Returns: Nothing
  */
        
        public void paintComponent(Graphics g){

                final int STARTX = 306;
                final int STARTY = 400;
                final int LINELENGTH = 240;
                final int GSTARTX = 20;
                final int GSTARTY = 450;
                double dEndX;
                double dEndY;
                double gnomon = Math.toRadians(calc.getGnomonAngle());


                super.paintComponent(g);
                g.setColor(Color.BLACK);
                
                // Draws Sundial
                for(int i = -6;i < 6;i++)
                {
                        dEndX = STARTX +(LINELENGTH * Math.sin(Math.toRadians(calc.getHourLine((i)))));
                        dEndY = STARTY -(LINELENGTH * Math.cos(Math.toRadians(calc.getHourLine((i)))));
                        
                        g.drawLine(STARTX,STARTY,(int)dEndX,(int)dEndY);
                        if(calc.isDST()){
                        if(i < 0) 
                                g.drawString(String.valueOf(i + 13), (int)dEndX, (int)dEndY - 15);
                        else
                            g.drawString(String.valueOf(i + 1), (int)dEndX, (int)dEndY -15);
                        }
                        else{
                                if(i <= 0) 
                                g.drawString(String.valueOf(i + 12), (int)dEndX, (int)dEndY - 15);
                        else
                            g.drawString(String.valueOf(i), (int)dEndX, (int)dEndY -15);
                        }
                }
                
                // Changes dial orientaion if in the Southern Hemisphere
                
                if(gnomon >= 0){
                	g.drawString("AM", STARTX-LINELENGTH,STARTY + 15);
                	g.drawString("PM", STARTX+LINELENGTH-15,STARTY + 15);
                
                	g.drawString("N",25,25);
                }
                else{                
                	g.drawString("PM", STARTX-LINELENGTH,STARTY + 15);
                	g.drawString("AM", STARTX+LINELENGTH-15,STARTY + 15);
                
                	g.drawString("S",25,25);
                	
                	
                }
                g.drawLine(28, 30, 28, 50);
                g.drawLine(28, 30, 23, 40);
                g.drawLine(28,30,33,40);
                
                //Creates gnomon
                
                if(gnomon < 0)
                        gnomon = -gnomon;

                g.drawLine(GSTARTX, GSTARTY, (int)(GSTARTX +LINELENGTH*Math.tan(gnomon)),GSTARTY+LINELENGTH);
                g.drawLine(GSTARTX, GSTARTY+ LINELENGTH,(int)(GSTARTX+LINELENGTH*Math.tan(gnomon)),GSTARTY + LINELENGTH);
      
                g.setColor(Color.RED);
                g.drawLine(GSTARTX, GSTARTY, GSTARTX,GSTARTY+LINELENGTH);
                
                //Guidelines
                g.drawLine(STARTX, STARTY, STARTX, (STARTY - LINELENGTH+50));
                g.drawLine((STARTX-LINELENGTH),STARTY, (STARTX + LINELENGTH),STARTY);
        }

}